package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //新增菜品
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);

        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor->flavor.setDishId(id));
        //新增口味
        if (flavors!=null && flavors.size()>0){
            dishFlavorsMapper.addFlavors(flavors);
        }

    }

    //菜品分页查询
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //删除菜品
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断当前菜品是否有起售中的菜品，如果有则不能删除
        for (Long id : ids) {
            Integer status = dishMapper.checkStatus(id);
            if (status == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断当前菜品是否关联套餐，如果关联则不能删除
        List<Long> setmeals = setmealDishMapper.checkSetmealByDishId(ids);
        if (setmeals!=null && setmeals.size()>0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品
        dishMapper.delete(ids);

        //删除菜品关联的口味
        dishFlavorsMapper.deleteById(ids);
    }

    //根据id查询菜品信息和口味
    @Override
    public DishVO checkDishById(Long id) {
        Dish dish = dishMapper.checkDishById(id);
        List<DishFlavor> dishFlavor = dishFlavorsMapper.checkFlavorById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    //修改菜品
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        //修改菜品表的信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        //修改菜品关联的口味信息(逻辑为先删除口味再增加口味)
        List<Long> dishId = new ArrayList<>();
        dishId.add(dishDTO.getId());
        dishFlavorsMapper.deleteById(dishId);

        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor->flavor.setDishId(id));
        if (flavors!=null && flavors.size()>0){
            dishFlavorsMapper.addFlavors(flavors);
        }
    }

    //启售或禁售菜品
    @Override
    public void setDishStatus(Integer status,Long id) {
        dishMapper.setDishStatus(status,id);
    }

    //根据分类id查询菜品
    @Override
    public Dish[] dishByCategoryId(Long categoryId) {
        Dish[] dish = dishMapper.dishByCategoryId(categoryId);
        return dish;
    }

    //根据分类id查询菜品和口味
    @Override
    public List<DishVO> dishAndFlavorByCategoryId(Long categoryId) {
        List<DishVO> list = new ArrayList<>();
        Dish[] dishs = dishMapper.dishByCategoryId(categoryId);
        for (Dish dish : dishs) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            List<DishFlavor> dishFlavors = dishFlavorsMapper.checkFlavorById(dish.getId());
            dishVO.setFlavors(dishFlavors);
            list.add(dishVO);
        }
        return list;
    }
}

